package top.canyue.word_range_service.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import top.canyue.word_range.pojo.po.Review;
import top.canyue.word_range.pojo.po.WordMapping;

import java.util.List;

@Mapper
public interface ReviewMapper extends BaseMapper<Review> {

    @Select("SELECT wm.* " +
            "FROM ( " +
            "  SELECT word " +
            "  FROM review " +
            "  WHERE  " +
            "    user_id = #{userId} " +
            "    AND review.next_review_date <= NOW() " +
            "    AND review.stage > 0 " +
            "  ORDER BY next_review_date, stage DESC , RAND() " +
            "  LIMIT #{count} " +
            ") AS sub " +
            "JOIN word_mapping wm ON sub.word = wm.word " +
            "WHERE wm.book_id = #{bookId} ")
    List<WordMapping> getRandomReviewWordList(String userId, String bookId, Integer count);

    // NOT EXISTS 性能比 NOT IN 好
    @Select("SELECT wm.* " +
            "FROM word_mapping wm " +
            "WHERE wm.book_id = #{bookId} " +
            "AND NOT EXISTS ( " +
            "    SELECT 1 " +
            "    FROM review r " +
            "    WHERE r.user_id = #{userId} " +
            "    AND r.word = wm.word " +
            ") " +
            "ORDER BY RAND() " +
            "LIMIT #{count} ")
    List<WordMapping> getRandomNewWordList(String userId, String bookId, Integer count);

    @Select("SELECT wm.* " +
            "FROM ( " +
            "  SELECT word " +
            "  FROM review " +
            "  WHERE  " +
            "    user_id = 'e77b59eef1370374608604f21bfb6140' " +
            "  ORDER BY RAND() " +
            "  LIMIT 10 " +
            ") AS sub " +
            "JOIN word_mapping wm ON sub.word = wm.word ")
    List<WordMapping> getRandomReviewWordListByEvent(String userId, Integer count);

}
